package algocasts;

public class P30_editDistance {
    private int min(int a, int b, int c) {
        return Math.min(a, Math.min(b, c));
    }

    public int editDistance(String s, String t) {
        if (s == null || t == null) return 0;
        int m = s.length() + 1, n = t.length() + 1;

        int[][] d = new int[m][n];
        for (int i = 0; i < m; i++) d[i][0] = i;
        for (int j = 0; j < n; j++) d[0][j] = j;

        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                if (s.charAt(i - 1) == t.charAt(j - 1))
                    d[i][j] = d[i - 1][j - 1];
                else {
                    d[i][j] = min(d[i - 1][j - 1], d[i - 1][j], d[i][j - 1]) + 1;
                }
            }
        }
        return d[m - 1][n - 1];
    }
}
